package CommodityService

import (
	"QingDai-Gin/api/models"
	"QingDai-Gin/config"
	"gorm.io/gorm"
)

func GetAllCommodity() ([]models.Commodity, *gorm.DB) {
	var commodities []models.Commodity
	Mysql := config.Mysql()
	tx := Mysql.Table("commodity__commodity").Find(&commodities)
	return commodities, tx
}

func GetCommodityById(id int) (models.Commodity, *gorm.DB) {
	var commodity models.Commodity
	Mysql := config.Mysql()
	tx := Mysql.Table("commodity__commodity").First(&commodity, id)
	return commodity, tx
}

func GetCommoditiesByP(p map[string]any) ([]models.Commodity, *gorm.DB) {
	var commodities []models.Commodity
	Mysql := config.Mysql()
	tx := Mysql.Table("commodity__commodity")
	for key, value := range p {
		tx.Where(key+" = ?", value)
	}
	tx.Find(&commodities)
	return commodities, tx
}
func GetCommodityByP(p map[string]any) (models.Commodity, *gorm.DB) {
	var commodity models.Commodity
	Mysql := config.Mysql()
	tx := Mysql.Table("commodity__commodity")
	for key, value := range p {
		tx.Where(key+" = ?", value)
	}
	tx.First(&commodity)
	return commodity, tx
}

func CreateCommodity(commodity models.Commodity) {
	Mysql := config.Mysql()
	Mysql.Table("commodity__commodity").Create(commodity)
}

func UpdateCommodity(commodity models.Commodity) {
	Mysql := config.Mysql()
	Mysql.Table("commodity__commodity").Save(commodity)
}

func DeleteCommodity(commodity models.Commodity) {
	Mysql := config.Mysql()
	Mysql.Table("commodity__commodity").Delete(commodity)
}
